home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / Pascal OS8 / AMReminder / MainMenu.p < prev    next >
Encoding:
Text File  |  1998-10-30  |  4.9 KB  |  256 lines  |  [TEXT/CWIE]

  1. { MainMenu.p }
  2. { Created 10/30/98 1:03 PM by AppMaker }
  3.  
  4. Unit MainMenu;
  5. Interface
  6.  
  7. Uses
  8.     Types,
  9.     Quickdraw,
  10.     Controls,
  11.     Dialogs,
  12.     Events,
  13.     Lists,
  14.     LowMem,
  15.     Menus,
  16.     TextEdit;
  17.  
  18. {----------}
  19. Procedure InitTitles;
  20. Procedure LoadMenus;
  21. Procedure DoMenu        (menuChoice:    longint);
  22. Procedure UpdateMenus;
  23.  
  24. {----------}
  25. Implementation
  26.  
  27. Uses
  28.     Devices,
  29.     Globals,
  30.     ResourceDefs,
  31.     Miscellany,
  32.     AMApp,
  33.     AMDoc,
  34.     AMEngine,
  35.     AMWindow;
  36.  
  37. {----------}
  38. Function  GetCommandFromMenu (
  39.     menuChoice:    longint): longint; Forward;
  40. Procedure DoApple (
  41.     itemNr:        integer); Forward;
  42.  
  43. {----------}
  44. Procedure InitTitles;
  45. Begin
  46. end; {InitTitles}
  47.  
  48. {----------}
  49. Procedure LoadMenus;
  50. Begin
  51.     AppleMenu    := GetMenu (MENU_Apple);
  52.     FailNilResource (Handle (AppleMenu));
  53.     AppendResMenu (AppleMenu, 'DRVR');
  54.     FileMenu    := GetMenu (MENU_File);
  55.     EditMenu    := GetMenu (MENU_Edit);
  56.     RemindMenu    := GetMenu (MENU_Remind);
  57.  
  58.     InsertMenu (AppleMenu, 0);
  59.     InsertMenu (FileMenu, 0);
  60.     InsertMenu (EditMenu, 0);
  61.     InsertMenu (RemindMenu, 0);
  62.  
  63.     DrawMenuBar;
  64. end; {LoadMenus}
  65.  
  66. {----------}
  67. Procedure DoMenu (
  68.     menuChoice:        longint);
  69. var
  70.     commandID:        longint;
  71.     curDoc:            AMDoc;
  72.     menuID:            integer;
  73.     itemNr:            integer;
  74. begin
  75.     commandID := GetCommandFromMenu (menuChoice);
  76.     curDoc := cur.mDoc;
  77.     if cur.DoCommand (commandID) then begin
  78.         { cur window handled it }
  79.     end else if (curDoc <> nil)
  80.     & (curDoc.DoCommand (commandID)) then begin
  81.         { document handled it }
  82.     end else if gApplication.DoCommand (commandID) then begin
  83.         { application handled it }
  84.     end else begin
  85.         menuID := HiWrd (menuChoice);
  86.         itemNr := LoWrd (menuChoice);
  87.         if menuID = MENU_Apple then begin
  88.             DoApple (itemNr);
  89.         end;
  90.     end;
  91.     HiliteMenu (0);
  92. end; {DoMenu}
  93.  
  94. {----------}
  95. Function  GetCommandFromMenu (
  96.     menuChoice:        longint): longint;
  97. var
  98.     commandID:        longint;
  99. Begin
  100.     commandID := 0;
  101.  
  102.     case menuChoice of
  103.         cAppleAbout:
  104.                 commandID := cmdAbout;
  105.         cFileNew:
  106.                 commandID := cmdNew;
  107.         cFileOpen:
  108.                 commandID := cmdOpen;
  109.         cFileClose:
  110.                 commandID := cmdClose;
  111.         cFileSave:
  112.                 commandID := cmdSave;
  113.         cFileSaveAs:
  114.                 commandID := cmdSaveAs;
  115.         cFileRevert:
  116.                 commandID := cmdRevert;
  117.         cFilePageSetup:
  118.                 commandID := cmdPageSetup;
  119.         cFilePrint:
  120.                 commandID := cmdPrint;
  121.         cFileQuit:
  122.                 commandID := cmdQuit;
  123.         cEditUndo:
  124.                 commandID := cmdUndo;
  125.         cEditCut:
  126.                 commandID := cmdCut;
  127.         cEditCopy:
  128.                 commandID := cmdCopy;
  129.         cEditPaste:
  130.                 commandID := cmdPaste;
  131.         cEditClear:
  132.                 commandID := cmdClear;
  133.         cEditSelectAll:
  134.                 commandID := cmdSelectAll;
  135.         cEditShowClipboard:
  136.                 commandID := cmdShowClipboard;
  137.  
  138.         otherwise
  139.                 commandID := -menuChoice;
  140.     end;
  141.  
  142.     GetCommandFromMenu := commandID;
  143. End;
  144.  
  145. {----------}
  146. Procedure DoApple (
  147.     itemNr:            integer);
  148. var
  149.     name:            Str255;
  150.     refNum:            integer;
  151. Begin
  152.     GetMenuItemText (AppleMenu, itemNr, name);
  153.     refNum := OpenDeskAcc (name);
  154. End;
  155.  
  156. {----------}
  157. var
  158.     menu:                MenuHandle;
  159.     menuBarChanged:        Boolean;
  160.  
  161. {----------}
  162. Procedure Enable (
  163.     itemNr:        integer;
  164.     enabled:    Boolean);
  165. Begin
  166.     if enabled then begin
  167.         EnableItem  (menu, itemNr);
  168.     end else begin
  169.         DisableItem (menu, itemNr);
  170.     end;
  171. end; {Enable}
  172.  
  173. {----------}
  174. Procedure EnableTitle (
  175.     menu:        MenuHandle;
  176.     enabled:    Boolean);
  177. Begin
  178.     if enabled <> BTst (menu^^.enableFlags, 0) then begin
  179.         menuBarChanged := true;
  180.     end;
  181.     if enabled then begin
  182.         EnableItem  (menu, 0);
  183.     end else begin
  184.         DisableItem (menu, 0);
  185.     end;
  186. end; {EnableTitle}
  187.  
  188. {----------}
  189. Procedure UpdateMenus;
  190. var
  191.     frontPeek:        WindowPeek;
  192.     isFront:        Boolean;    {is there a front window?}
  193.     isCur:            Boolean;    {is there a current window?}
  194.     isCurDoc:        Boolean;    {is there a current document?}
  195.     isDirty:        Boolean;    {is it dirty?}
  196.     hasFile:        Boolean;    {does it have a file?}
  197.     isSelected:        Boolean;    {is anything selected?}
  198.     isDesk:            Boolean;    {is the front window a desk acc?}
  199.     isText:            Boolean;    {is there a current text field?}
  200.     isScrap:        Boolean;    {is there any scrap?}
  201.     curTE:            TEHandle;
  202. Begin
  203.     menuBarChanged := false;
  204.  
  205.     isFront        := (FrontWindow () <> nil);
  206.     isCur        := (curWindow <> nil);
  207.     isDirty        := false;
  208.     hasFile        := false;
  209.     isSelected    := false;
  210.     isCurDoc    := cur.mDoc <> nil;
  211.     if isCurDoc then begin
  212.         isDirty        := cur.mDoc.mEngine.IsDirty;
  213.         hasFile        := cur.mDoc.mEngine.HasFile;
  214.     end;
  215.  
  216.     isDesk := false;
  217.     if isFront then begin
  218.         frontPeek    := WindowPeek (FrontWindow);
  219.         isDesk        := (frontPeek^.windowKind < 0);
  220.     end;
  221.  
  222.     curTE := cur.GetCurTE;
  223.  
  224.     isText        := isCur & (curTE <> nil);
  225.     isScrap        := false;
  226.     if isText then begin
  227.         isSelected    := (curTE^^.selStart <> curTE^^.selEnd);
  228.         isScrap        := (TEGetScrapLength > 0);
  229.     end;
  230.  
  231.     menu := FileMenu;
  232.     Enable (cFileClose,            isFront);
  233.     Enable (cFileSave,            isDirty);
  234.     Enable (cFileSaveAs,        isCurDoc);
  235.     Enable (cFileRevert,        isDirty);
  236.  
  237.     menu := EditMenu;
  238.     if isFront then begin
  239.         Enable (cEditUndo,        isDesk);
  240.         Enable (cEditCut,        isDesk | isSelected);
  241.         Enable (cEditCopy,        isDesk | isSelected);
  242.         Enable (cEditPaste,        isDesk | isScrap);
  243.         Enable (cEditClear,        isDesk | isSelected);
  244.         Enable (cEditSelectAll,    isText);
  245.  
  246.     end;
  247.     EnableTitle (EditMenu,     isFront);
  248.  
  249.  
  250.     if menuBarChanged then begin
  251.         DrawMenuBar;
  252.     end;
  253. end; {UpdateMenus}
  254.  
  255. end.
  256.